package com.aa.controller;

import com.aa.entity.Goods;
import com.aa.entity.Goodstype;
import com.aa.entity.Users;
import com.aa.service.impl.GoodsServiceImpl;
import com.aa.service.impl.GoodstypeServiceImpl;
import com.aa.service.impl.UsersServiceImpl;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
@Controller
@RequestMapping("/admin")
public class UsersCtrl {
    @Resource
    UsersServiceImpl usersService;

    @Resource
    GoodstypeServiceImpl goodstypeService;
    @ApiOperation("获取所有商品")
    @RequestMapping(value = "/users",method = {RequestMethod.POST,RequestMethod.GET})
    public String findAllGoods(Model model,Users users,
                               @RequestParam(value = "pageNum",required = false, defaultValue = "1") Integer pageNum
            , @RequestParam(value = "pageSize",required = false, defaultValue = "1") Integer pageSize){

        PageInfo<Users> usersList =  usersService.queryAll( users,pageNum,pageSize);
        model.addAttribute("usersListPage",usersList);
        return "usersList";
    }
    @ApiOperation("根据ID删除商品")
    @GetMapping("/delUsers/{id}")
    public String delUsers(@PathVariable("id") int id, Model model){
        usersService.deleteById(id);
        model.addAttribute("msg","删除成功");
        return "forward:/admin/users";
    }
    // 通过request对象实现转发
//    @GetMapping("/delGoods/{id}")
//    public void delGoods(@PathVariable("id") int id, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        int num = goodsService.deleteById(id);
//        request.setAttribute("msg","删除成功！");
//        request.getRequestDispatcher("/goods").forward(request,response);
//    }

    // 通过ModelAndView对象实现转发
//    @GetMapping("/delGoods/{id}")
//    public ModelAndView delGoods(@PathVariable("id") int id) {
//        int num = goodsService.deleteById(id);
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.addObject("msg","删除成功！");
//        modelAndView.setViewName("forward:/goods");
//        return modelAndView;
//    }

    @GetMapping("/users/{id}")
    public String getUsersById(@PathVariable("id") int id,Model model){
        Users users = usersService.queryById(id);
        model.addAttribute("users",users);
        return "usersEdit";
    }


    @PostMapping("/editUsers")
    public String addUsers(Users users,Model model){

        System.out.println(users);
         usersService.update(users);
        model.addAttribute("msg","商品信息更新成功！");
        return "forward:/admin/users";
    }

    @GetMapping("/gotoAddUsers")
    public String gotoAddusers(){  return "usersAdd";

    }

    @PostMapping("/addUsers")
    public String addUsers(Users users){
        Users num = usersService.insert(users);
        return "forward:/admin/goods";
    }



}
